//
//  MainViewController.swift
//  Sina
//
//  Created by 袁量 on 2016/11/28.
//  Copyright © 2016年 袁量. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

     lazy var composeBtn : UIButton = UIButton(imageName: "tabbar_compose_icon_add", backgroundImageName: "tabbar_compose_button")
    override func viewDidLoad() {
        super.viewDidLoad()
        setUpComposeBtn()
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }
}

// MARK: - 设置 UI 界面
extension MainViewController{
    /// 设置 发布按钮
    func setUpComposeBtn() {
        tabBar.addSubview(composeBtn)
        composeBtn.center = CGPoint(x: tabBar.center.x, y: tabBar.bounds.size.height * 0.5)
        composeBtn.addTarget(self, action: #selector(MainViewController.composeClick), for: .touchUpInside)
    }
}

// MARK: - 响应 UI 事件
extension MainViewController{

     /// 监听发布按钮
     func composeClick() {
//        WWLog(mesage: "监听 + 按钮")
        let composeVC = ComposeViewController()
        let composeNav = UINavigationController(rootViewController: composeVC)
        present(composeNav, animated: true) {}
        
    }
}
// MARK: - 之前的纯代码创建界面
extension MainViewController{
    private func oldCode() {
        // MARK:  -内部控制方法
        //    private func addChildViewControllers() {
        //
        //
        ////        WWLog(mesage: "子控制器添加完成")
        //        //        1. 根据 JSON 文件创建控制
        //
        //        //        1.1.读取 JSON 数据
        //        //        1.2 将 JSON 数据转换为对象 （数据字典）
        //        //        1.3 便利数组字典去除每一个字典
        //        //        1.4 根据遍历的字典创建控制器
        //
        //        guard let filePath = Bundle.main.path(forResource: "MainVCSettings", ofType: "json") else
        //        {
        //            WWLog(mesage: "文件加载不成功")
        //            return
        //        }
        //        WWLog(mesage: filePath)
        //        guard let data = NSData(contentsOfFile: filePath) else
        //        {
        //            WWLog(mesage: "加载二进制数据失败")
        //            return
        //        }
        //        do
        //        {
        //
        //             let objc = try JSONSerialization.jsonObject(with: data as Data, options: JSONSerialization.ReadingOptions.mutableContainers) as! [[String: AnyObject]]//[[String: AnyObject]]数组字典
        //            WWLog(mesage: objc)
        //
        //            for dict in objc
        //            {
        //                WWLog(mesage: dict)
        //                let title = dict["title"] as? String
        //                let vcName = dict["vcName"] as? String
        //                let imageName = dict["imageName"] as? String
        //                addChildViewController(childControllerName: vcName, title: title, imageName: imageName)
        //            }
        //        }
        //        catch
        //        {
        //            WWLog(mesage: "try 异常")
        //            addChildViewController(childControllerName: "HomeViewController", title: "首页", imageName: "tabbar_home")
        //            addChildViewController(childControllerName: "MessageViewController", title: "消息", imageName: "tabbar_message_center")
        //            addChildViewController(childControllerName: "NullViewController", title: "", imageName: "")
        //            addChildViewController(childControllerName: "DiscoverViewController", title: "发现", imageName: "tabbar_discover")
        //            addChildViewController(childControllerName: "ProfileViewController", title: "我", imageName: "tabbar_profile")
        //        }
        //}
        
        //    private func addChildViewController(childControllerName: String?, title: String?, imageName: String?)
        //    {
        //
        //        guard let name = Bundle.main.infoDictionary!["CFBundleExecutable"] as? String else
        //
        //        {
        //            WWLog(mesage: "获取命名空间失败")
        //            return
        //        }
        //        var cls: AnyClass? = nil
        //        if let vcName = childControllerName
        //        {
        //            cls = NSClassFromString( name + "." + vcName)
        //        }
        //         //let cls: AnyClass? = NSClassFromString( name + "." + childControllerName)
        //        WWLog(mesage: cls)
        //
        //        guard let typeCls = cls as? UITableViewController.Type else
        //        {
        //            WWLog(mesage: "cls 不能当作 UITableViewController")
        //            return
        //        }
        //
        //        let childController = typeCls.init()
        //        WWLog(mesage: childController)
        //        childController.tabBarItem.title = title
        //        childController.navigationItem.title = title
        //        if let imaName = imageName {
        //            childController.tabBarItem.image = UIImage(named: imaName)
        //            childController.tabBarItem.selectedImage = UIImage(named: imaName + "highlighted")
        //        }
        //        
        //        let nav = UINavigationController(rootViewController: childController)
        //        
        //        addChildViewController(nav)
        //        
        //    }
    }
}
